//
//  CrashManager.m
//  BaseLib
//
//  Created by CPX on 2018/5/22.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "CrashManager.h"
#import "CocoaLumberjack.h"
#import "AvoidCrash.h"
#import "LogManager.h"

/**
 崩溃管理
 @author yzl
 */
@implementation CrashManager

+ (void)registerApp{
    [self defaultManager];
}

+ (instancetype)defaultManager{
    static CrashManager *mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mgr = [CrashManager new];
        [mgr _init];
    });
    return mgr;
}

- (void)_init{
    [AvoidCrash makeAllEffective];
    
    NSArray *noneSelClassStrings = @[
                                     @"NSNull",
                                     @"NSNumber",
                                     @"NSString",
                                     @"NSDictionary",
                                     @"NSArray"
                                     ];
    [AvoidCrash setupNoneSelClassStringsArr:noneSelClassStrings];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
}

- (void)dealwithCrashMessage:(NSNotification *)note {
    //异常拦截并且通过bugly上报
    
    NSDictionary *info = note.userInfo;
    NSString *errorReason = [NSString stringWithFormat:@"【ErrorReason】%@========【ErrorPlace】%@========【DefaultToDo】%@========【ErrorName】%@", info[@"errorReason"], info[@"errorPlace"], info[@"defaultToDo"], info[@"errorName"]];
    NSArray *callStack = info[@"callStackSymbols"];
    DDLogError(errorReason);
    DDLogError(@"%@",callStack);
}

@end
